library(magrittr)
library(plyr)
library(tidyverse)
library(readxl)
library(vietnamcode)
library(car)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


dv_survey = paste0(home, 'Data/survey-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         Missing=as.integer(is.na(Q1))) %>%
  subset(!is.na(Treatment))


dv_pooled = paste0(home, 'Data/pooled-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm'))) %>%
  subset(!is.na(Treatment)) %>%
  arrange(Province, Name_VN)


df_delegate_raw = paste0(home, 'Data/14th-VNA-membership.csv') %>%
  read_csv %>%
  mutate(Province=mapvalues(x=province, 
                            from=vietnamcode_data$province_name_diacritics, 
                            to=vietnamcode_data$province_name,
                            warn_missing=F)) %>%
  mutate(Province=as.character(Province))


df_delegate_raw$Province[str_detect(string=df_delegate_raw$province, 
                                    pattern='a - V')] = 'BRVT'
df_delegate_raw = arrange(.data=df_delegate_raw, Province, name)
df_delegate_raw$ID = llply(.data=df_delegate_raw$name, 
                           .fun=function(x) {
                             found = which(str_detect(string=dv_pooled$Name_VN, pattern=x))
                             if(length(found)<1) {
                               NA
                             } else if(length(found)==1) {
                               dv_pooled$ID[found]
                             } else {
                               999L
                             }
                           }) %>% unlist
df_delegate_raw$ID[is.na(df_delegate_raw$ID) | df_delegate_raw$ID==999L] = as.integer(c(59,57,157,257,149,201,233,134,133,455,449,115,262,260,197,120,33,369,215,88,298,174,312,422,418,419,69,393,137,221,109,253,NA,22,404,92,9,234,236,19,331,163,514,302,358,81))


df_delegate_raw_survey = merge(x=df_delegate_raw, 
                               y=subset(dv_survey, 
                                        select=-c(Province,
                                                  Dosage)), 
                               by='ID', 
                               all.x=T) %>%
  subset(!is.na(ID))


df_delegate_raw_survey$Prop.Treated = df_delegate_raw_survey$Prop.Citizen + df_delegate_raw_survey$Prop.Firm
df_delegate_raw_survey$Dosage = 0.5
df_delegate_raw_survey$Dosage[df_delegate_raw_survey$Prop.Treated==0] = 0
df_delegate_raw_survey$Dosage[df_delegate_raw_survey$Prop.Treated==1] = 1


df_delegate_raw_survey$Bucket = '000 Control'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Control' & df_delegate_raw_survey$Dosage==0.5] = '050 Control'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Citizen' & df_delegate_raw_survey$Dosage==0.5] = '050 Citizen'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Citizen' & df_delegate_raw_survey$Dosage==1] = '100 Citizen'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Firm' & df_delegate_raw_survey$Dosage==0.5] = '050 Firm'
df_delegate_raw_survey$Bucket[df_delegate_raw_survey$Treatment=='Firm' & df_delegate_raw_survey$Dosage==1] = '100 Firm'
df_delegate_raw_survey$Bucket = factor(x=df_delegate_raw_survey$Bucket, 
                                       levels=c('000 Control','050 Control','050 Citizen','100 Citizen','050 Firm','100 Firm'))


df_delegate_raw_pooled = merge(x=df_delegate_raw, 
                               y=subset(dv_pooled, 
                                        select=-c(Province,
                                                  Dosage)), 
                               by='ID', 
                               all.x=T) %>%
  subset(!is.na(ID))


df_delegate_raw_pooled$Prop.Treated = df_delegate_raw_pooled$Prop.Citizen + df_delegate_raw_pooled$Prop.Firm
df_delegate_raw_pooled$Dosage = 0.5
df_delegate_raw_pooled$Dosage[df_delegate_raw_pooled$Prop.Treated==0] = 0
df_delegate_raw_pooled$Dosage[df_delegate_raw_pooled$Prop.Treated==1] = 1


df_delegate_raw_pooled$Bucket = '000 Control'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Control' & df_delegate_raw_pooled$Dosage==0.5] = '050 Control'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Citizen' & df_delegate_raw_pooled$Dosage==0.5] = '050 Citizen'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Citizen' & df_delegate_raw_pooled$Dosage==1] = '100 Citizen'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Firm' & df_delegate_raw_pooled$Dosage==0.5] = '050 Firm'
df_delegate_raw_pooled$Bucket[df_delegate_raw_pooled$Treatment=='Firm' & df_delegate_raw_pooled$Dosage==1] = '100 Firm'
df_delegate_raw_pooled$Bucket = factor(x=df_delegate_raw_pooled$Bucket, 
                                       levels=c('000 Control','050 Control','050 Citizen','100 Citizen','050 Firm','100 Firm'))


fit_1 = lm(Q1 ~ 0 + Bucket, data=df_delegate_raw_survey)
fit_2 = lm(Spoke ~ 0 + Bucket, data=df_delegate_raw_pooled)


table_A08_2 = ldply(.data=c('Bucket050 Citizen = Bucket050 Control',
                            'Bucket050 Firm = Bucket050 Control',
                            'Bucket000 Control = Bucket050 Control',
                            'Bucket100 Citizen = Bucket050 Citizen',
                            'Bucket100 Firm = Bucket050 Firm'),
                    .fun=function(x) {
        tmp_1 = linearHypothesis(model=fit_1, x)
        tmp_2 = linearHypothesis(model=fit_2, x)
        data.frame(Fstat_1=tmp_1$F[2],
                   pvalue_1=tmp_1$`Pr(>F)`[2],
                   Fstat_2=tmp_2$F[2],
                   pvalue_2=tmp_2$`Pr(>F)`[2])
      }, .inform=T) %>%
  sapply(round, digits=3) %>%
  as.data.frame %>%
  set_rownames(c('Direct (50% Citizen vs. 50% Control)',
                 'Direct (50% Firm vs. 50% Control)',
                 'Spillover (100% Control vs. 50% Control)',
                 'Reinforcement (100% Citizen vs. 50% Citizen)',
                 'Reinforcement (100% Firm vs. 50% Firm)'))


stargazer(table_A08_2,
          summary=F,
          align=T,
          header=F,
          no.space=T, 
          title='Spillover and reinforcement effects not reflected in saturation analysis.',
          label='tab:ftests',
          notes.append=F, 
          notes='F-tests for equality of coefficients from Table 8.1.') %>%
  writeClipboard

